package com.huanbao.oil.app.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextClock;
import android.widget.Toast;

import com.google.gson.Gson;
import com.huanbao.oil.app.R;
import com.huanbao.oil.app.TCP.Data_syn;
import com.huanbao.oil.app.TCP.TCP_client;
import com.huanbao.oil.app.TCP.TCP_service;
import com.huanbao.oil.app.application.BaseApplication;
import com.huanbao.oil.app.application.MessageEvent;
import com.huanbao.oil.app.application.UserEvent;
import com.huanbao.oil.app.commons.CommonUtils;
import com.huanbao.oil.app.commons.JsonUtil;
import com.huanbao.oil.app.commons.SharedPreferencesUtils;
import com.huanbao.oil.app.commons.UtilsLog;
import com.huanbao.oil.app.model.MainModel;
import com.huanbao.oil.app.model.Model_13;
import com.huanbao.oil.app.model.Model_5;
import com.huanbao.oil.app.model.Model_7;
import com.huanbao.oil.app.model.PntIdNameModel;
import com.huanbao.oil.app.model.PntsBaseModel;
import com.huanbao.oil.app.model.SearchModel;
import com.huanbao.oil.app.model.SearchModel_13;
import com.huanbao.oil.app.model.SearchModel_7;
import com.huanbao.oil.app.socket.data.HandShake;
import com.huanbao.oil.app.socket.data.MsgDataBean;
import com.xuhao.didi.core.iocore.interfaces.IPulseSendable;
import com.xuhao.didi.core.iocore.interfaces.ISendable;
import com.xuhao.didi.core.pojo.OriginalData;
import com.xuhao.didi.socket.client.sdk.OkSocket;
import com.xuhao.didi.socket.client.sdk.client.ConnectionInfo;
import com.xuhao.didi.socket.client.sdk.client.OkSocketOptions;
import com.xuhao.didi.socket.client.sdk.client.action.SocketActionAdapter;
import com.xuhao.didi.socket.client.sdk.client.connection.IConnectionManager;
import com.xuhao.didi.socket.client.sdk.client.connection.NoneReconnect;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends FragmentActivity implements EasyPermissions.PermissionCallbacks {

    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.rb_1)
    RadioButton rb1;
    @BindView(R.id.rb_2)
    RadioButton rb2;
    @BindView(R.id.rb_3)
    RadioButton rb3;
    @BindView(R.id.rb_4)
    RadioButton rb4;
    @BindView(R.id.rb_5)
    RadioButton rb5;
    @BindView(R.id.rb_6)
    RadioButton rb6;
    @BindView(R.id.home_radio_group)
    RadioGroup homeRadioGroup;
    @BindView(R.id.ac_main_top_right_date)
    TextClock acMainTopRightDate;
    @BindView(R.id.ac_main_top_right_time)
    TextClock acMainTopRightTime;

    private Fragment mFragment;

    /**
     * 管理fragment
     */
    private HashMap<Integer, Fragment> fragments = new HashMap<>();

    //当前activity的fragment控件
    private int fragmentContentId = R.id.content;

    /**
     * 设置默认的fragment
     */
    private int currentTab = 0;

    boolean IsStart = false;//系统是否开始 true：开始  false：结束
    boolean IsLogin = false;//是否登陆 true：登录  false：没有登录


    //所要申请的权限
    String[] perms = {Manifest.permission.INTERNET};

    /**
     * socket通信相关
     */
    private ConnectionInfo mInfo;
    private IConnectionManager mManager;
    private OkSocketOptions mOkOptions;
    private String netIp = "";//配置的socket网址
    private String duanKou = "-1";//配置的socket端口

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        getPingMu();
//        homeRadioGroup.setOnCheckedChangeListener(this);
//        if (savedInstanceState != null)
//            mFragment = (Fragment) getSupportFragmentManager().getFragment(
//                    savedInstanceState, "mFragment");
        initFrag();
        defaultFragment();
        SelectColor(0);
        // 设置12时制显示格式
//        acMainTopRightDate.setFormat12Hour("MMMM dd, yyyy h:mmaa");
        // 设置24时制显示格式
        acMainTopRightDate.setFormat24Hour("MMMM dd, yyyy h:mmaa");
        acMainTopRightDate.setVisibility(View.GONE);
        // 设置12时制显示格式
        acMainTopRightTime.setFormat12Hour("yyyy-MM-dd hh:mm:ss");
        // 设置24时制显示格式
//        acMainTopRightTime.setFormat24Hour("hh:mm:ss");

        if (EasyPermissions.hasPermissions(this, perms)) {//检查是否获取该权限
            UtilsLog.e("zzl=已获取权限");
        } else {
            //第二个参数是被拒绝后再次申请该权限的解释
            //第三个参数是请求码
            //第四个参数是要申请的权限
            EasyPermissions.requestPermissions(this, "必要的权限", 0, perms);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (SharedPreferencesUtils.getIPModel(this) != null) {
            if (!TextUtils.isEmpty(SharedPreferencesUtils.getIPModel(this).getIp()
            ) && !TextUtils.isEmpty(SharedPreferencesUtils.getIPModel(this).getIp_port())
            ) {
                netIp = SharedPreferencesUtils.getIPModel(this).getIp();
                duanKou = SharedPreferencesUtils.getIPModel(this).getIp_port();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = new Message();
                        msg.arg1 = 1;
                        handler.sendMessage(msg);
                    }
                }).start();
                setSocketData(3);
            } else {
                Toast.makeText(this, "请设置IP端口号", Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(this, "请设置IP端口号", Toast.LENGTH_LONG).show();
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        //这是默认的方法
        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1) {
                case 1:
                    //link socket
                    initManager(netIp, duanKou);
                    connect();
                    fasong();
                    break;
                case 2:
                    String str = (String) msg.obj;
                    openDoor(str);
                    break;
                case 3:
//                    mManager.send(new HandShake("握手"));
                    break;
                case 4:
                    chongxinlianjie();
                    break;
//                case 7:
//                    setSocketData13();
//                    break;
            }
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    private void initFrag() {
        fragments.put(0, new HomeFragment());
        fragments.put(2, new YunXingJiLuFragment());
//        fragments.put(1, new YunXingCanShuFragment());
        fragments.put(3, new StatementFragment());
        fragments.put(4, new BaoJingJiLuFragment());
        fragments.put(5, new SheZhiCanShuFragment());
    }

    private void defaultFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(fragmentContentId, fragments.get(0));
        currentTab = 0;
        ft.commit();
    }

    /**
     * 当页面选中时改变当前的导航栏蓝色和图片的状态
     *
     * @param position 当前页面
     */
    public void SelectColor(int position) {
        if (position == 0) {
            rb1.setBackgroundResource(R.drawable.my_shape_blue);
            rb2.setBackgroundResource(R.drawable.my_shape);
            rb3.setBackgroundResource(R.drawable.my_shape);
            rb4.setBackgroundResource(R.drawable.my_shape);
            rb5.setBackgroundResource(R.drawable.my_shape);
            rb6.setBackgroundResource(R.drawable.my_shape);
        }
//        else if (position == 1) {
//            rb1.setBackgroundResource(R.drawable.my_shape);
//            rb2.setBackgroundResource(R.drawable.my_shape_blue);
//            rb3.setBackgroundResource(R.drawable.my_shape);
//            rb4.setBackgroundResource(R.drawable.my_shape);
//            rb5.setBackgroundResource(R.drawable.my_shape);
//            rb6.setBackgroundResource(R.drawable.my_shape);
//        }
        else if (position == 2) {
            rb1.setBackgroundResource(R.drawable.my_shape);
            rb2.setBackgroundResource(R.drawable.my_shape);
            rb3.setBackgroundResource(R.drawable.my_shape_blue);
            rb4.setBackgroundResource(R.drawable.my_shape);
            rb5.setBackgroundResource(R.drawable.my_shape);
            rb6.setBackgroundResource(R.drawable.my_shape);
        } else if (position == 3) {
            rb1.setBackgroundResource(R.drawable.my_shape);
            rb2.setBackgroundResource(R.drawable.my_shape);
            rb3.setBackgroundResource(R.drawable.my_shape);
            rb4.setBackgroundResource(R.drawable.my_shape_blue);
            rb5.setBackgroundResource(R.drawable.my_shape);
            rb6.setBackgroundResource(R.drawable.my_shape);
        } else if (position == 4) {
            rb1.setBackgroundResource(R.drawable.my_shape);
            rb2.setBackgroundResource(R.drawable.my_shape);
            rb3.setBackgroundResource(R.drawable.my_shape);
            rb4.setBackgroundResource(R.drawable.my_shape);
            rb5.setBackgroundResource(R.drawable.my_shape_blue);
            rb6.setBackgroundResource(R.drawable.my_shape);
        } else if (position == 5) {
            rb1.setBackgroundResource(R.drawable.my_shape);
            rb2.setBackgroundResource(R.drawable.my_shape);
            rb3.setBackgroundResource(R.drawable.my_shape);
            rb4.setBackgroundResource(R.drawable.my_shape);
            rb5.setBackgroundResource(R.drawable.my_shape);
            rb6.setBackgroundResource(R.drawable.my_shape_blue);
        }
    }

    /**
     * 点击切换下部按钮
     *
     * @param page
     */
    private void changeTab(int page) {
        //默认的currentTab == 当前的页码，不做任何处理
        if (currentTab == page) {
            return;
        }
        //获取fragment的页码
        Fragment fragment = fragments.get(page);
        //fragment事务
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        //如果该Fragment对象被添加到了它的Activity中，那么它返回true，否则返回false。
        //当前activity中添加的不是这个fragment
        if (!fragment.isAdded()) {
            //所以将他加进去
            ft.add(fragmentContentId, fragment);
        }
        //隐藏当前currentTab的
        ft.hide(fragments.get(currentTab));
        //显示现在page的
        ft.show(fragments.get(page));
        //设置当前currentTab底部的状态
        SelectColor(currentTab);
        //当前显示的赋值给currentTab
        currentTab = page;
        //设置当前currentTab底部的状态
        SelectColor(currentTab);
        //activity被销毁？  ！否
        if (!this.isFinishing()) {
            //允许状态丢失
            ft.commitAllowingStateLoss();
        }
    }

    public String currentName = "工程师";

    void showDialog() {
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.my_dialog, null);
        final EditText editTextName = textEntryView.findViewById(R.id.editTextName);
        final EditText editTextNumEditText = textEntryView.findViewById(R.id.editTextNum);
        final RadioGroup radioGroup = textEntryView.findViewById(R.id.rg_group);
        AlertDialog.Builder ad1 = new AlertDialog.Builder(this);
        ad1.setTitle("请输入账号密码:");
//        ad1.setIcon(android.R.drawable.ic_dialog_info);
        ad1.setView(textEntryView);
        ad1.setCancelable(false);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_gongchengshi:
                        Log.e("tag", "点击了第一个RadioButton:工程师");
                        currentName = "工程师";
                        break;
                    case R.id.rb_caozuoyuan:
                        Log.e("tag", "点击了第二个RadioButton:操作员");
                        currentName = "操作员";
                        break;
                }

                EventBus.getDefault().post(new UserEvent(currentName));
            }
        });

        ad1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
//                String name = editTextName.getText().toString().trim();
                String pwd = editTextNumEditText.getText().toString().trim();
                Log.e("tag", currentName + "===" + pwd);
                if ("工程师".equals(currentName) && "111111".equals(pwd)) {
                    IsLogin = true;
//                    EventBus.getDefault().post(new UserEvent(currentName));
                    Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                } else if ("操作员".equals(currentName) && "222222".equals(pwd)) {
                    IsLogin = true;
//                    EventBus.getDefault().post(new UserEvent(currentName));
                    Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
                    dialog.dismiss();
                } else {
                    IsLogin = false;
                    Toast.makeText(MainActivity.this, "密码错误，请重试", Toast.LENGTH_LONG).show();
                }
            }
        });
        ad1.create().setCanceledOnTouchOutside(false);
        ad1.show();// 显示对话框
    }


    final static int COUNTS = 5;// 点击次数
    final static long DURATION = 1000;// 规定有效时间
    long[] mHits = new long[COUNTS];

    private void continuousClick(int count, long time) {
        //每次点击时，数组向前移动一位
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        //为数组最后一位赋值
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis() - DURATION)) {
            mHits = new long[COUNTS];//重新初始化数组
            Intent intent = new Intent(MainActivity.this, SettingActivity.class);
            startActivity(intent);
//            Toast.makeText(this, "连续点击了5次", Toast.LENGTH_LONG).show();
        }
    }

    @OnClick({R.id.ac_main_top_right_date, R.id.ac_main_top_right_time, R.id.rb_1,
            R.id.rb_2, R.id.rb_3, R.id.rb_4, R.id.rb_5, R.id.rb_6})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_main_top_right_date:
                break;
            case R.id.ac_main_top_right_time:
                //setting
                continuousClick(COUNTS, DURATION);
                break;
            case R.id.rb_1:
                TCP_MODE = 0;
                //主界面
                if (IsLogin) {
                    changeTab(0);
                    setSocketData(3);
//                    setSocketData13();
                } else {
                    showDialog();
                }
                break;
            case R.id.rb_2:
//                setSocketData(3);
                TCP_MODE = 1;
                //运行参数
                if (IsLogin) {
                    changeTab(1);
                } else {
                    showDialog();
                }
                break;
            case R.id.rb_3:
                //运行记录
//                setSocketData(11);
                TCP_MODE = 2;
                if (IsLogin) {
                    changeTab(2);
                } else {
                    showDialog();
                }
                break;
            case R.id.rb_4:
                //状态显示
//                setSocketData(1);
                TCP_MODE = 3;
                if (IsLogin) {
                    changeTab(3);
                    setSocketData3();
                } else {
                    showDialog();
                }
                break;
            case R.id.rb_5:
                //报警记录
                TCP_MODE = 4;
                if (IsLogin) {
                    changeTab(4);
                } else {
                    showDialog();
                }
                break;
            case R.id.rb_6:
//                setSocketData(3);
//                setSocketData7();
                //得到一部分参数设置的值
                TCP_MODE = 5;
                //参数设置
                if (IsLogin) {
                    if ("工程师".equals(currentName)) {
                        changeTab(5);
//                        setSocketData15();
//                        setSocketData1000();
//                        Handler handler = new Handler();
//                        handler.postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                setSocketData5();
//                            }
//                        }, 300);//后执行Runnable中的run方法
//
//                        setSocketData7();
                    } else {
                        Toast.makeText(this, "没有操作权限", Toast.LENGTH_LONG).show();
                    }
                } else {
                    showDialog();
                }
                break;
        }
    }

    /**
     * 获取屏幕分辨率
     */
    private int width, height;//宽度，高度
    private int swidth, sheight;//屏幕宽度，高度

    @SuppressLint("NewApi")
    private void getPingMu() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        swidth = dm.widthPixels;
        sheight = dm.heightPixels;
        UtilsLog.e("当前屏幕的分辨率宽" + swidth + ",高度" + sheight + "" + ",手机dpi====" + dm.densityDpi + "");
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //把申请权限的回调交由EasyPermissions处理
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    //下面两个方法是实现EasyPermissions的EasyPermissions.PermissionCallbacks接口
    //分别返回授权成功和失败的权限
    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Log.i("", "获取成功的权限" + perms);
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        Log.i("", "获取失败的权限" + perms);
    }


    //---------------留在最底下---------------socekt通信相关------------------------------------------------------
    //==============================================================================================
    //==============================================================================================
    private void initManager(String netIp, String duanKou) {
        mInfo = new ConnectionInfo(netIp, Integer.parseInt(duanKou));
        mOkOptions = new OkSocketOptions.Builder()
                .setReconnectionManager(new NoneReconnect())
                .setWritePackageBytes(1024)
                .setConnectionHolden(true)
                .setConnectTimeoutSecond(10 * 1000)
//                .setCallbackInThread(false)
                .build();
        mManager = OkSocket.open(mInfo).option(mOkOptions);
        mManager.registerReceiver(adapter);
    }

    /**
     * 链接socket
     */
    void connect() {
        if (mManager == null) {
            return;
        }
        if (!mManager.isConnect()) {
            initManager(netIp, duanKou);
            mManager.connect();
//            CrashReport.postCatchedException(new Exception("链接socket"));
        }
    }

    /**
     * 重新链接socket
     */
    void chongxinlianjie() {
        UtilsLog.e("zzl=chongxinlianjie----");
        if (!CommonUtils.isOnline(this)) {
            return;
        }
        if (mManager == null) {
            return;
        }
        if (!mManager.isConnect()) {
            initManager(netIp, duanKou);
            try {
                mManager.connect();
            } catch (Exception e) {
                UtilsLog.e("mManager连接异常===" + e.toString());
                UtilsLog.e("mManager连接异常===" + e.getMessage());
//                new Handler().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        chongxinlianjie();
//                    }
//                }, 2 * 1000);
            }
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    chongxinlianjie();
                }
            }, 2 * 60 * 1000);
        }
    }

    /**
     * 发送心跳包
     */
    void fasong() {
        if (mManager == null) {
            return;
        }
        if (!mManager.isConnect()) {
            UtilsLog.e("zzl=未启动服务先启动服务");
            chongxinlianjie();
        } else {
//            CrashReport.postCatchedException(new Exception("发送心跳包"));
            String msg = "发送心跳包";
            if (TextUtils.isEmpty(msg.trim())) {
                return;
            }
//            MsgDataBean msgDataBean = new MsgDataBean(msg);
//            mManager.send(msgDataBean);
//            mManager.send(new HandShake(msg));
        }
    }

    /**
     * 发送心跳包
     */
    void openDoor(String jsonStr) {
        UtilsLog.e("zzl=发送数据=" + jsonStr);
        if (mManager == null) {
            return;
        }
        if (!mManager.isConnect()) {
            UtilsLog.e("zzl=未启动服务先启动服务");
            chongxinlianjie();
        } else {
            if (TextUtils.isEmpty(jsonStr.trim())) {
                return;
            }
            MsgDataBean msgDataBean = new MsgDataBean(jsonStr);
            mManager.send(msgDataBean);

        }
    }

    /**
     * socekt通信处理
     */
    private SocketActionAdapter adapter = new SocketActionAdapter() {

        @Override
        public void onSocketConnectionSuccess(ConnectionInfo info, String action) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message msg = new Message();
                    msg.arg1 = 3;
                    msg.obj = jsonStr;
                    handler.sendMessage(msg);
                }
            }).start();
            UtilsLog.e("zzl=成功连接");
        }

        @Override
        public void onSocketDisconnection(ConnectionInfo info, String action, Exception e) {
            if (e != null) {
//                logSend("异常断开:" + e.getMessage());
                UtilsLog.e("zzl=异常断开:" + e.getMessage());
            } else {
                UtilsLog.e("zzl=正常断开");
            }
            UtilsLog.e("zzl=断开了需要重新链接");
//            chongxinlianjie();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message msg = new Message();
                    msg.arg1 = 4;
                    msg.obj = jsonStr;
                    handler.sendMessage(msg);
                }
            }).start();
        }

        @Override
        public void onSocketConnectionFailed(ConnectionInfo info, String action, Exception e) {
//            UtilsLog.e("zzl=连接失败，需要重新链接");
//            jxJson("");
//            chongxinlianjie();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message msg = new Message();
                    msg.arg1 = 4;
                    msg.obj = jsonStr;
                    handler.sendMessage(msg);
                }
            }).start();
        }

        @Override
        public void onSocketReadResponse(ConnectionInfo info, String action, OriginalData data) {
//            Log.e("tag", "action==" + action);
//            Log.e("tag", "onSocketReadResponse==" + data.getBodyBytes());
//            Log.e("tag", "onSocketReadResponse==" + data.toString());
            String str = new String(data.getBodyBytes(), StandardCharsets.UTF_8);
//            logRece(str);
            Log.e("tag", "onSocketReadResponse==str===" + str);
            EventBus.getDefault().postSticky(new MessageEvent(str));
        }

        @Override
        public void onSocketWriteResponse(ConnectionInfo info, String action, ISendable data) {
//            Log.e("tag", "action=onSocketWriteResponse=" + action);
//            Log.e("tag", "onSocketWriteResponse==" + data.toString());
            String str = new String(data.parse(), StandardCharsets.UTF_8);
//            logSend(str);
//            Log.e("tag", "onSocketWriteResponse==str===" + str);
        }

        @Override
        public void onPulseSend(ConnectionInfo info, IPulseSendable data) {
            String str = new String(data.parse(), StandardCharsets.UTF_8);
//            logSend(str);
            Log.e("tag", "onPulseSend==str===" + str);
        }
    };


    private void logSend(final String log) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            UtilsLog.e("zzl=发送的内容" + log);
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    logSend("非UI线程打印:" + log);
                }
            });
        }
    }

    private void logRece(final String log) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            UtilsLog.e("zzl=接收的内容" + log);
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    logRece("非UI线程打印:" + log);
                }
            });
        }

    }
    //---------------留在最底下---------------------------------------------------------------------
    //==============================================================================================
    //==============================================================================================


    //----------------------↓↓↓↓↓↓↓↓↓↓↓↓socket方案↓↓↓↓↓↓↓↓↓-------------
    //==============================================================================================
    //==============================================================================================
    //网络连接模式选择
    public final static int MODE_TCP_SERVER = 0;
    public final static int MODE_TCP_CLIENT = 1;
    public int ch_mode = 1;
    //TCP服务器通信模式下
    public TCP_service tcp_service = null;
    // TCP客户端通信模式下
    public TCP_client tcp_client = null;
    public final static int CLIENT_STATE_CORRECT_READ = 7;
    public final static int CLIENT_STATE_CORRECT_WRITE = 8;               //正常通信信息
    public final static int CLIENT_STATE_ERROR = 9;                 //发生错误异常信息
    public final static int CLIENT_STATE_IOFO = 10;                  //发送SOCKET信息
    public boolean client_islink = false;

    //复选状态信息
    public boolean Hex_show = false;//接受数据是否16进制
    public boolean Hex_send = false;//发送数据是否16进制
    //计数用
    public int countin = 0;
    public int countout = 0;

    String re_data_show = "";
    String jsonStr = null;
    public int TCP_MODE = 0;//0：主页面 1：运行参数 2：运行记录 3：状态显示 4：报警记录 5：参数设置

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (tcp_service != null) {
//            tcp_service.setis_start(false);
//            tcp_service.close();
//            tcp_service = null;
//        }
        if (tcp_client != null) {
            tcp_client.close();
            tcp_client = null;
        }
    }

    //开始连接tcp
    public void startTCP() {
        if (tcp_client == null) {
            tcp_client = new TCP_client(cli_handler);
            try {
                InetAddress ipAddress = InetAddress.getByName
                        (SharedPreferencesUtils.getIPModel(this).getIp());
                int port = Integer.valueOf(SharedPreferencesUtils.getIPModel(this).getIp_port());//获取端口号
                tcp_client.setInetAddress(ipAddress);
                tcp_client.setPort(port);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            tcp_client.start();
        }
    }

    //客户端通信模式下
    public Handler cli_handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CLIENT_STATE_ERROR:
//                    Toast.makeText(instances, "连接异常"
//                            , Toast.LENGTH_SHORT).show();
//                    ip_show.setHint("对象IP");
//                    name_show.setHint("对象主机名");
                    client_islink = false;
                    UtilsLog.e("socket方案====TCP客户端模式 连接异常");
                    break;
                case CLIENT_STATE_IOFO:
                    client_islink = true;
                    String[] strings = (String[]) msg.obj;
//                    ip_show.append(strings[0] + "\n");
//                    name_show.append(strings[1] + "\n");
                    UtilsLog.e("socket方案====TCP客户端模式   建立连接" + strings);
                    break;
                //接收数据
                case CLIENT_STATE_CORRECT_READ:
                    Handler_receive(msg);
                    break;
                //发送数据
                case CLIENT_STATE_CORRECT_WRITE:
                    Handler_send(msg);
                    break;
            }
        }
    };


    //发送数据函数
    public void sendmessage(String message) {
        if (Hex_send == true) {
            byte[] send = Data_syn.hexStr2Bytes(message);
            if (ch_mode == MODE_TCP_SERVER) {
                tcp_service.write(send);
            } else if (ch_mode == MODE_TCP_CLIENT) {
                tcp_client.sendmessage(send);
            }
        } else {
            //非16进制
            byte[] send = message.getBytes();
            if (ch_mode == MODE_TCP_SERVER) {
                tcp_service.write(send);
            } else if (ch_mode == MODE_TCP_CLIENT) {
                tcp_client.sendmessage(send);
            }
        }
    }

    // 接收数据处理分析函数，通过handler从子线程回传到主线程
    public void Handler_receive(Message msg) {
        byte[] buffer = (byte[]) msg.obj;
        if (Hex_show == true) {
            String readMessage = " "
                    + Data_syn.bytesToHexString(buffer, msg.arg1);
            re_data_show = readMessage;
            UtilsLog.e("socket方案====re_data_show=1111===" + re_data_show);
            countin += readMessage.length() / 2;                               // 接收计数
//            UtilsLog.e("socket方案====re_count" + countin + "个");
        } else if (Hex_show == false) {
            //非16进制
            String readMessage = null;
            readMessage = new String(buffer, 0, msg.arg1, StandardCharsets.UTF_8);
            re_data_show = readMessage;
//            EventBus.getDefault().postSticky(new MessageEvent(re_data_show));
            UtilsLog.e("socket方案====re_data_show=2222===" + re_data_show);
            countin += readMessage.length();                                   // 接收计数
//            UtilsLog.e("socket方案====re_count" + countin + "个");
            if (!TextUtils.isEmpty(re_data_show)) {
                EventBus.getDefault().post(new MessageEvent("yyyyyyyyyyyy"));
                if (re_data_show.trim().equals("1")) {
                    Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show();
                } else if (re_data_show.trim().equals("0")) {
                    Toast.makeText(this, "发送失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                UtilsLog.e("socket方案====服务器没有返回值");
            }
        }
    }

    //发送数据处理分析函数，通过handler从子线程回传主线程
    public void Handler_send(Message msg) {
        byte[] writeBuf = (byte[]) msg.obj;
        if (Hex_send == true) {
            String writeMessage = Data_syn.Bytes2HexString(writeBuf);
            countout += writeMessage.length() / 2;
            UtilsLog.e("" + countin + "个");
        } else if (Hex_send == false) {
            //非16进制
            String writeMessage = null;
            UtilsLog.e("socket方案====writeMessage" + writeMessage);
            writeMessage = new String(writeBuf, StandardCharsets.UTF_8);
            countout += writeMessage.length();
//            UtilsLog.e("socket方案====se_count" + countin + "个");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserEvent(UserEvent event) {
        UtilsLog.e("MainActivity===接收到的数据==onUserEvent::" + event.getMessage());

    }

    List<MainModel> modelList = new ArrayList<>();

    @Subscribe(threadMode = ThreadMode.ASYNC, sticky = true)
    public void onMessageEvent(MessageEvent event) {
        UtilsLog.e("MainActivity===接收到的数据==onMessageEvent::" + event.getMessage());
        UtilsLog.e("MainActivity=====" + TCP_MODE);
        if (TCP_MODE == 0) {
//            UtilsLog.e("主页面接收到的数据==" + event.getMessage());
            if (!TextUtils.isEmpty(event.getMessage()) && !"{}".equals(event.getMessage())
                    && !"{[]}".equals(event.getMessage())) {
                try {
                    JSONObject jsonObject = new JSONObject(event.getMessage());
                    int cmd_type = 0;
                    if (jsonObject.has("cmd_type")) {
                        cmd_type = jsonObject.getInt("cmd_type");
                        UtilsLog.e("主页面cmd_type==" + cmd_type);
                    }
                    if (cmd_type == 2) {
//                        UtilsLog.e("主页面进入解析==");
                        if (jsonObject.getString("pnts") != "[]" && !TextUtils.isEmpty(jsonObject.getString("pnts"))) {
                            modelList = JsonUtil.stringToArray(jsonObject.getString("pnts"), MainModel[].class);
                            if (modelList != null && modelList.size() > 0) {
//                                UtilsLog.e("主页面pnts==");
//                                new Thread(new Runnable() {
//                                    @Override
//                                    public void run() {
////                                        UtilsLog.e("主页面handler==");
//                                        Message msg = new Message();
//                                        msg.obj = modelList;
//                                        handler_main.sendMessage(msg);
//                                    }
//                                }).start();
                            } else {
                                UtilsLog.e("主页面零条数据==");
                            }
                        }
                    } else {
                        UtilsLog.e("主页面 cmd_type!==2");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    UtilsLog.e("主页面进入解析==" + e.getMessage());
                }
            } else {
                UtilsLog.e("主页面没有获取到数据==");
            }
        } else {
            UtilsLog.e("主页面000000000==");
        }
    }

    //    List<MainModel> mainModelList = new ArrayList<>();
    private Handler handler_main = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.obj != null) {
                List<MainModel> lList = (List<MainModel>) msg.obj;
                for (int i = 0; i < lList.size(); i++) {
                    HashMap<String, String> map = getDataHashMap();
                    if (!TextUtils.isEmpty(map.get(String.valueOf(lList.get(i).getPnt_id())))) {
                        map.put(String.valueOf(lList.get(i).getPnt_id()), String.valueOf(lList.get(i).getValue()));
                    } else {
                        UtilsLog.e("主页面HashMap==数据为空");
                    }
                }
//                Set<String> set = dataHashMap.keySet();
//                for (String string : set) {
//                    UtilsLog.e("====="+string + " " + dataHashMap.get(string));
//                }
            } else {
//                UtilsLog.e("主页面22222222222==");
            }
        }
    };
    //-----------------------↑↑↑↑↑↑↑↑↑↑socket方案↑↑↑↑↑↑↑↑↑↑--------------
    //==============================================================================================
    //==============================================================================================

    public HashMap<String, String> dataHashMap = new HashMap<String, String>() {
        {
            put("1", "0.0");
            put("2", "0.0");
            put("3", "0.0");
            put("4", "0.0");
            put("5", "0.0");
            put("6", "0.0");
            put("7", "0.0");
            put("8", "0.0");
            put("9", "0.0");
            put("10", "0.0");
            put("11", "0.0");
            put("12", "0.0");
            put("13", "0.0");
            put("14", "0.0");
            put("15", "0.0");
            put("16", "0.0");
            put("17", "0.0");
            put("18", "0.0");
            put("19", "0.0");
            put("20", "0.0");
            put("21", "0.0");
            put("22", "0.0");
            put("23", "0.0");
            put("24", "0.0");
            put("25", "0.0");
            put("26", "0.0");
            put("27", "0.0");
            put("28", "0.0");
            put("29", "0.0");
            put("30", "0.0");
            put("31", "0.0");
            put("32", "0.0");
            put("33", "0.0");
            put("34", "0.0");
            put("35", "0.0");
            put("36", "0.0");
            put("37", "0.0");
            put("38", "0.0");
            put("39", "0.0");
            put("40", "0.0");
            put("41", "0.0");
            put("42", "0.0");
            put("43", "0.0");
            put("44", "0.0");
            put("45", "0.0");
            put("46", "0.0");
            put("47", "0.0");
            put("48", "0.0");
            put("49", "0.0");
            put("50", "0.0");
            put("51", "0.0");
            put("52", "0.0");
            put("53", "0.0");
            put("54", "0.0");
            put("55", "0.0");
            put("56", "0.0");
            put("57", "0.0");
            put("58", "0.0");
            put("59", "0.0");
            put("60", "0.0");
            put("200", "0.0");
            put("201", "0.0");
            put("202", "0.0");
            put("203", "0.0");
            put("204", "0.0");
            put("205", "0.0");
            put("206", "0.0");
            put("207", "0.0");
            put("208", "0.0");
            put("209", "0.0");
            put("210", "0.0");
            put("211", "0.0");
            put("212", "0.0");
            put("213", "0.0");
            put("214", "0.0");
            put("215", "0.0");
            put("216", "0.0");
            put("500", "0.0");
            put("501", "0.0");
            put("502", "0.0");
            put("503", "0.0");
            put("504", "0.0");
            put("505", "0.0");
            put("506", "0.0");
            put("507", "0.0");
            put("508", "0.0");
            put("509", "0.0");
            put("510", "0.0");
            put("511", "0.0");
            put("512", "0.0");
            put("513", "0.0");
            put("514", "0.0");
            put("515", "0.0");
            put("516", "0.0");
            put("517", "0.0");

            put("100", "0.0");
            put("101", "0.0");
            put("102", "0.0");
            put("103", "0.0");
            put("104", "0.0");
            put("105", "0.0");
            put("106", "0.0");
            put("107", "0.0");
            put("108", "0.0");
            put("109", "0.0");
            put("110", "0.0");
            put("111", "0.0");
            put("112", "0.0");
            put("113", "0.0");
            put("114", "0.0");
            put("115", "0.0");
            put("116", "0.0");
            put("117", "0.0");

            put("1000", "0.0");
            put("1001", "0.0");
            put("1002", "0.0");
            put("1003", "0.0");
            put("1004", "0.0");
            put("1005", "0.0");
            put("1006", "0.0");
            put("1007", "0.0");
            put("1008", "0.0");
            put("2000", "0.0");
            put("2001", "0.0");
            put("3000", "0.0");
        }
    };

    public HashMap<String, String> getDataHashMap() {
        return dataHashMap;
    }

    void setSocketData(int k) {
        SearchModel model = new SearchModel();
        model.setCmd_type(k);
        List<PntsBaseModel> pntsModels = new ArrayList<>();
        PntsBaseModel pntsModel = null;
        for (int i = 1; i < 8; i++) {
            pntsModel = new PntsBaseModel();
            pntsModel.setPnt_id(i);
            pntsModels.add(pntsModel);
        }
//        model.setPnts(pntsModels);

        PntsBaseModel pntsModel2 = new PntsBaseModel();
        pntsModel2.setPnt_id(1000);
        pntsModels.add(pntsModel2);
        model.setPnts(pntsModels);
        PntsBaseModel pntsModel3 = new PntsBaseModel();
        pntsModel3.setPnt_id(1008);
        pntsModels.add(pntsModel3);
        model.setPnts(pntsModels);
        Gson gson = new Gson();
        jsonStr = gson.toJson(model);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = jsonStr;
                handler.sendMessage(msg);
            }
        }).start();
    }

    void setSocketData1000() {
        SearchModel model = new SearchModel();
        model.setCmd_type(5);
        List<PntsBaseModel> pntsModels = new ArrayList<>();
        PntsBaseModel pntsModel = null;
        for (int i = 0; i < BaseApplication.pids.length; i++) {
            pntsModel = new PntsBaseModel();
            pntsModel.setPnt_id(BaseApplication.pids[i]);
            pntsModels.add(pntsModel);
        }
        model.setPnts(pntsModels);
        Gson gson = new Gson();
        jsonStr = gson.toJson(model);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = jsonStr;
                handler.sendMessage(msg);
            }
        }).start();
    }

    void setSocketData3() {
        SearchModel model = new SearchModel();
        model.setCmd_type(3);
        List<PntsBaseModel> pntsModels = new ArrayList<>();
        PntsBaseModel pntsModel = null;
        for (int i = 0; i < BaseApplication.pids.length; i++) {
            pntsModel = new PntsBaseModel();
            pntsModel.setPnt_id(BaseApplication.pids[i]);
            pntsModels.add(pntsModel);
        }
        model.setPnts(pntsModels);
        Gson gson = new Gson();
        jsonStr = gson.toJson(model);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = jsonStr;
                handler.sendMessage(msg);
            }
        }).start();
    }

    void setSocketData5() {
        Model_5 model = new Model_5();
        model.setCmd_type(5);
        List<PntIdNameModel> pntsModels = new ArrayList<PntIdNameModel>();
        PntIdNameModel pntsModel = null;
        for (int i = 0; i < BaseApplication.pid_ids.length; i++) {
            pntsModel = new PntIdNameModel();
            pntsModel.setPnt_id(BaseApplication.pid_ids[i]);
            pntsModel.setPnt_para(BaseApplication.pid_names[i]);
            pntsModels.add(pntsModel);
        }
        model.setPnts(pntsModels);
        Gson gson = new Gson();
        jsonStr = gson.toJson(model);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = jsonStr;
                handler.sendMessage(msg);
            }
        }).start();
    }

    void setSocketData13() {
        SearchModel_13 model = new SearchModel_13();
        model.setCmd_type(13);
        List<Model_13> pntsModels = new ArrayList<>();
        Model_13 pntsModel = null;
        for (int i = 1; i < 5; i++) {
            pntsModel = new Model_13();
            pntsModel.setPnt_id(i);
            pntsModel.setPnt_name("DI1");
            pntsModel.setHigh_level_value(1);
            pntsModels.add(pntsModel);
        }
        model.setPnts(pntsModels);
        Gson gson = new Gson();
        jsonStr = gson.toJson(model);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = jsonStr;
                handler.sendMessage(msg);
            }
        }).start();
    }

    void setSocketData7() {
        SearchModel_7 model = new SearchModel_7();
        model.setCmd_type(7);
        List<Model_7> pntsModels = new ArrayList<>();
        Model_7 pntsModel = null;
        pntsModel = new Model_7();
        pntsModel.setPara_name("Para.YSJDelayStartTime");
        pntsModels.add(pntsModel);
        model.setParas(pntsModels);

        Model_7 pntsModel2 = null;
        pntsModel2 = new Model_7();
        pntsModel2.setPara_name("Para.YSJDelayStopTime");
        pntsModels.add(pntsModel2);
        model.setParas(pntsModels);

        Model_7 pntsModel3 = null;
        pntsModel3 = new Model_7();
        pntsModel3.setPara_name("Para.SysMaintainSetTimeHour");
        pntsModels.add(pntsModel3);
        model.setParas(pntsModels);

        Model_7 pntsModel4 = null;
        pntsModel4 = new Model_7();
        pntsModel4.setPara_name("Para.RongShuangTime");
        pntsModels.add(pntsModel4);
        model.setParas(pntsModels);

        Model_7 pntsModel5 = null;
        pntsModel5 = new Model_7();
        pntsModel5.setPara_name("Para.DiShuiTime");
        pntsModels.add(pntsModel5);
        model.setParas(pntsModels);

        Model_7 pntsModel6 = null;
        pntsModel6 = new Model_7();
        pntsModel6.setPara_name("Para.RongShuangJianGeTime");
        pntsModels.add(pntsModel6);
        model.setParas(pntsModels);

        Model_7 pntsModel7 = null;
        pntsModel7 = new Model_7();
        pntsModel7.setPara_name("Para.WorkTime");
        pntsModels.add(pntsModel7);
        model.setParas(pntsModels);

        Model_7 pntsModel8 = null;
        pntsModel8 = new Model_7();
        pntsModel8.setPara_name("Para.FreeTime");
        pntsModels.add(pntsModel8);
        model.setParas(pntsModels);

        Model_7 pntsModel9 = null;
        pntsModel9 = new Model_7();
        pntsModel9.setPara_name("Para.RongShuangYouXiaoTime");
        pntsModels.add(pntsModel9);
        model.setParas(pntsModels);

        Model_7 pntsMode20 = null;
        pntsMode20 = new Model_7();
        pntsMode20.setPara_name("Para.DBRWorkTime");
        pntsModels.add(pntsMode20);
        model.setParas(pntsModels);

        Model_7 pntsMode21 = null;
        pntsMode21 = new Model_7();
        pntsMode21.setPara_name("Para.DBRFreeTimee");
        pntsModels.add(pntsMode21);
        model.setParas(pntsModels);
        Gson gson = new Gson();
        jsonStr = gson.toJson(model);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = jsonStr;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15 = "";

    void setSocketData15(Double a1, Double a2, Double a3) {
        str_15 = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.YSJDelayStartTime\": " + a1 +
                "}," +
                "{" +
                "\"Para.YSJDelayStopTime\": " + a2 +
                "}," +
                "{" +
                "\"Para.SysMaintainSetTimeHour\": " + a3 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String json_15_head = "{" +
            "\"cmd_type\":15," +
            "\"paras\":" +
            "[";
    String json_15_end = "]" + "}";

    String send15json(String pname, Double a1) {
        String str_15_json =
                "{" +
                        "\"" + pname + "\": " + a1 +
                        "},";
//        String str_ping =
//                "{" +
//                        "\"pnt_id\": " + pid + "," +
//                        "\"pnt_name\": " + "\"" + pname + "\"," +
//                        "\"" + valueName + "\": " + value +
//                        "},";
        return str_15_json;
    }

    void send15(final String json) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = json_15_head + json + json_15_end;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15_1 = "";

    void setSocketData15_1(Double a1) {
        str_15_1 = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.YSJDelayStartTime\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_1;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15_2 = "";

    void setSocketData15_2(Double a1) {
        str_15_2 = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.YSJDelayStopTime\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_2;
                handler.sendMessage(msg);
            }
        }).start();
    }


    String str_15_3 = "";

    void setSocketData15_3(Double a1) {
        str_15_3 = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.SysMaintainSetTimeHour\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_3;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15_xifu = "";

    void setSocketData15_xifu(Double a1) {
        str_15_xifu = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.FreeTime\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_xifu;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15_jiexi = "";

    void setSocketData15_jiexi(Double a1) {
        str_15_jiexi = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.WorkTime\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_jiexi;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15_rongshuang_jiange = "";

    void setSocketData15_rongshuang_jiange(Double a1) {
        str_15_rongshuang_jiange = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.RongShuangJianGeTime\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_rongshuang_jiange;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15_rongshuang_shijian = "";

    void setSocketData15_rongshuang_shijian(Double a1) {
        str_15_rongshuang_shijian = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.RongShuangTime\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_rongshuang_shijian;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_15_dishui_shijian = "";

    void setSocketData15_dishui_shijian(Double a1) {
        str_15_dishui_shijian = "{" +
                "\"cmd_type\":15," +
                "\"paras\":" +
                "[" +
                "{" +
                "\"Para.DiShuiTime\": " + a1 +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_15_dishui_shijian;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_13 = "";

    void str13(int pid, String pname, String valueName, String value) {
        UtilsLog.e("str13=" + "pid==" + pid + "==valueName==" + valueName + "==value==" + value);
        str_13 = "{" +
                "\"cmd_type\":13," +
                "\"pnts\":" +
                "[" +
                "{" +
                "\"pnt_id\": " + pid + "," +
//                "}," +
////                "{" +
                "\"pnt_name\": " + "\"" + pname + "\"," +
//                "}," +
//                "{" +
                "\"" + valueName + "\": " + value +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_13;
                handler.sendMessage(msg);
//                handler.postDelayed(this,1200);
            }
        }).start();
    }

    String json_head = "{" +
            "\"cmd_type\":13," +
            "\"pnts\":" +
            "[";
    String json_end = "]" +
            "}";

    String strping(int pid, String pname, String valueName, String value) {
        UtilsLog.e("strping=" + "pid==" + pid + "==valueName==" + valueName + "==value==" + value);
        String str_ping =
                "{" +
                        "\"pnt_id\": " + pid + "," +
                        "\"pnt_name\": " + "\"" + pname + "\"," +
                        "\"" + valueName + "\": " + value +
                        "},";
        return str_ping;
    }

    void send_strping(final String json) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = json_head + json + json_end;
                handler.sendMessage(msg);
            }
        }).start();
    }

    String str_13_wendu_1 = "";

    void str13_wendu_1(int pid, String pname, String valueName, String value) {
        UtilsLog.e("str_13_wendu_1==str13=" + "pid==" + pid + "==valueName==" + valueName + "==value==" + value);
        str_13_wendu_1 = "{" +
                "\"cmd_type\":13," +
                "\"pnts\":" +
                "[" +
                "{" +
                "\"pnt_id\": " + pid + "," +
//                "}," +
////                "{" +
                "\"pnt_name\": " + "\"" + pname + "\"," +
//                "}," +
//                "{" +
                "\"" + valueName + "\": " + value +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_13_wendu_1;
                handler.sendMessage(msg);
//                handler.postDelayed(this,1200);
            }
        }).start();
    }

    String str_13_wendu_2 = "";

    void str13_wendu_2(int pid, String pname, String valueName, String value) {
        UtilsLog.e("str_13_wendu_2==str13=" + "pid==" + pid + "==valueName==" + valueName + "==value==" + value);
        str_13_wendu_2 = "{" +
                "\"cmd_type\":13," +
                "\"pnts\":" +
                "[" +
                "{" +
                "\"pnt_id\": " + pid + "," +
//                "}," +
////                "{" +
                "\"pnt_name\": " + "\"" + pname + "\"," +
//                "}," +
//                "{" +
                "\"" + valueName + "\": " + value +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_13_wendu_2;
                handler.sendMessage(msg);
//                handler.postDelayed(this,1200);
            }
        }).start();
    }

    String str_13_wendu_3 = "";

    void str_13_wendu_3(int pid, String pname, String valueName, String value) {
        UtilsLog.e("str_13_wendu_3==str13=" + "pid==" + pid + "==valueName==" + valueName + "==value==" + value);
        str_13_wendu_3 = "{" +
                "\"cmd_type\":13," +
                "\"pnts\":" +
                "[" +
                "{" +
                "\"pnt_id\": " + pid + "," +
//                "}," +
////                "{" +
                "\"pnt_name\": " + "\"" + pname + "\"," +
//                "}," +
//                "{" +
                "\"" + valueName + "\": " + value +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_13_wendu_3;
                handler.sendMessage(msg);
//                handler.postDelayed(this,1200);
            }
        }).start();
    }

    String str_5 = "";

    void str5(int pid, String pname, String valueName, String value) {
        str_5 = "{" +
                "\"cmd_type\":13," +
                "\"pnts\":" +
                "[" +
                "{" +
                "\"pnt_id\": " + pid + "," +
//                "}," +
////                "{" +
                "\"pnt_name\": " + "\"" + pname + "\"," +
//                "}," +
//                "{" +
                "\"" + valueName + "\": " + value +
                "}" +
                "]" +
                "}";
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.arg1 = 2;
                msg.obj = str_13;
                handler.sendMessage(msg);
            }
        }).start();
    }
}
